<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Interview extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(null, true);
		$this->load->model('Interview_model', 'interview');
		$this->lang->load('interview', $this->session->userdata['user_lang']);
		$this->lang->load('portal', $this->session->userdata['user_lang']);
	}
	
	public function start($subscription_id = null)
	{
		if (is_numeric($subscription_id))
		{
			$this->load->model('Subscription_model', 'subscription');
			
			$subscription_data = $this->subscription->get(array('idsubscription' => $subscription_id))->row();
			
			if ($subscription_data->idcompany == $this->session->userdata['idprofile'])
			{
				$this->load->library("OpenTok/OpenTok");
				
				$data['idcompany'] = $subscription_data->idcompany;
				$data['idcandidate'] = $subscription_data->idcandidate;
				
				//arguments not necessary if you've set the API key in API_config.php in API_Config.php
				$OT = new OpenTok();
				
				$OT->generate_session_id();
				$OT->generate_token();
				
				$data['api_key'] = $OT->apiKey;
				$data['session_id'] = $OT->sessionId;
				$data['token'] = $OT->token;
				
				$this->interview->insert($data);
				
				$data['waiting_message'] = $this->lang->line('interview_waitingcandidate');
				$data['disconnect_message'] = $this->lang->line('interview_candidatedisconnect');
				$data['main_content'] = 'interview/video_chat';
				$data['userdata'] = $this->session->userdata;
		        $this->load->view('template-interview', $data);
			}
			else
			{
				redirect(base_url() . 'company');
			}
		}
	}
	
	public function enter($interview_id = null)
	{
		if (is_numeric($interview_id))
		{
			$interview_data = $this->interview->get(array('idinterview' => $interview_id))->row();
			
			$is_candidate = $interview_data->idcandidate == $this->session->userdata['idprofile'] && $this->session->userdata['idtype'] == 1;
			$is_company = $interview_data->idcompany == $this->session->userdata['idprofile'] && $this->session->userdata['idtype'] == 2;
			
			if ($is_candidate || $is_company)
			{
				$this->load->library("OpenTok/OpenTok");
				
				//arguments not necessary if you've set the API key in API_config.php in API_Config.php
				$OT = new OpenTok();
				
				$OT->generate_token();
				
				$data['api_key'] = $OT->apiKey;
				$data['session_id'] = $interview_data->idsession;
				$data['token'] = $OT->token;
				
				if ($is_candidate)
				{
					$data['waiting_message'] = $this->lang->line('interview_waitingcompany');
					$data['disconnect_message'] = $this->lang->line('interview_companydisconnect');
				}
				else
				{
					$data['waiting_message'] = $this->lang->line('interview_waitingcandidate');
					$data['disconnect_message'] = $this->lang->line('interview_candidatedisconnect');
				}
				
				$data['main_content'] = 'interview/video_chat';
				$data['userdata'] = $this->session->userdata;
		        $this->load->view('template-interview', $data);
			}
			else
			{
				redirect(base_url() . 'candidate');
			}
		}
	}
}

?>